var system = require('system');
var url  = system.args[4];
var file = system.args[5];
url = url || 'http://errorpage.b0.upaiyun.com/esm48-404';

console.log(url+" request");

var casper = require('casper').create({
        "pageSettings" : {
            loadImages: true,
            loadPlugins: false,
            javascriptEnabled: false,
            localToRemoteUrlAccessEnabled: false,
            resourceTimeout: 5000
        },
        timeout: 20000,
        logLevel: "error",//日志等级
        verbose: true,    // 记录日志到控制台
});

casper.on('timeout', function () {
    this.echo("===>timeout");
    casper.exit();
});

casper.on('resourceTimeout', function () {
    this.echo("===>resourceTimeout");
    casper.exit();
});

casper.start(url);
casper.start().viewport(350, 220).thenOpen(url, function() {
    this.capture(file, {
        top: 0,
        left: 0,
        width: 0,
        height: 0
    }, {
        format: 'jpg',
        quality: 100
    });
    this.echo(file+" saved.");
});

casper.run(function () {
    this.echo(this.getCurrentUrl().trim()+" request end");
    casper.exit();
});
